package com.atguigu.flink.day07;

import com.atguigu.flink.beans.WaterSensor;
import com.atguigu.flink.func.WaterSensorMapFunction;
import org.apache.flink.api.common.eventtime.SerializableTimestampAssigner;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.streaming.api.TimerService;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

import java.time.Duration;

/**
 * @author Felix
 * @date 2023/12/8
 * 该案例演示了Flink定时器
 */
public class Flink01_timer {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        SingleOutputStreamOperator<WaterSensor> wsDS = env
            .socketTextStream("hadoop102", 8888)
            .map(new WaterSensorMapFunction())
            .assignTimestampsAndWatermarks(
                WatermarkStrategy
                    .<WaterSensor>forMonotonousTimestamps()
                    .withTimestampAssigner(
                        new SerializableTimestampAssigner<WaterSensor>() {
                            @Override
                            public long extractTimestamp(WaterSensor ws, long recordTimestamp) {
                                return ws.getTs();
                            }
                        }
                    )
            );


        //只有KeyedStream的process才能使用定时器
        KeyedStream<WaterSensor, String> keyedDS = wsDS.keyBy(WaterSensor::getId);

        //对分组后的数据进行处理
        keyedDS.process(
            new KeyedProcessFunction<String, WaterSensor, String>() {
                @Override
                public void processElement(WaterSensor ws, Context ctx, Collector<String> out) throws Exception {
                    //获取分组key
                    String currentKey = ctx.getCurrentKey();
                    //获取当前元素的时间
                    Long timestamp = ctx.timestamp();

                    System.out.println("timeStamp:" + timestamp);
                    //定时服务
                    //主要提供的服务有3项
                    //注册定时器
                    //删除定时器
                    //获取处理时间/水位线
                    TimerService timerService = ctx.timerService();

                    //注册一个事件时间定时器
                    timerService.registerEventTimeTimer(5);

                    //注册一个处理时间定时器
                    // long currentProcessingTime = timerService.currentProcessingTime();
                    // timerService.registerProcessingTimeTimer(currentProcessingTime + 10000);

                    //删除定时器
                    // timerService.deleteEventTimeTimer(5);
                    // timerService.deleteProcessingTimeTimer(xxxx);

                    //获取水位线
                    System.out.println("水位线:" + timerService.currentWatermark());
                }

                //定时器被触发的时候，执行的方法
                @Override
                public void onTimer(long timestamp, OnTimerContext ctx, Collector<String> out) throws Exception {
                    String currentKey = ctx.getCurrentKey();
                    out.collect("key=" + currentKey + "现在时间是" + timestamp + "定时器触发");
                }
            }
        ).print();

        env.execute();

    }
}
